﻿using System;
using System.Management;

namespace ClusterAwareProperties
{
    /// <summary>
    /// Convert a network ManagementObject to Network Class
    /// </summary>
    public static class EventResourceStateChangeTranslate
    {
        /// <summary>
        /// Convert EventResourceStateChange ManagementObject to EventResourceStateChangeProperties Class
        /// </summary>
        /// <param name="clsevent">EventResourceStateChangeProperties class</param>
        /// <param name="eventObject">ManagementObject EventResourceStateChange</param>
        /// <returns>EventResourceStateChangeProperties class with properties converted</returns>
        public static EventResourceStateChangePropety Convert(this EventResourceStateChangePropety clsevent, ManagementBaseObject eventObject)
        {
            clsevent.EventGroup = eventObject.PropertyValue<string>("EventGroup");
            clsevent.EventNode = eventObject.PropertyValue<string>("EventNode");
            clsevent.EventObjectName = eventObject.PropertyValue<string>("EventObjectName");
            clsevent.EventObjectPath = eventObject.PropertyValue<string>("EventObjectPath");
            clsevent.EventObjectType = eventObject.PropertyEnum<UInt32, ObjectTypeEvent>("EventObjectType", ObjectTypeEvent.Unknown);
            clsevent.EventTypeMajor = eventObject.PropertyValue<UInt32>("EventTypeMajor");
            clsevent.EventTypeMinor = eventObject.PropertyValue<UInt32>("EventTypeMinor");
            clsevent.SecurityDescriptor = eventObject.PropertyValue<UInt16[]>("SECURITY_DESCRIPTOR");
            var objtime =  eventObject.PropertyValue<UInt64>("TIME_CREATED");
            clsevent.TimeCreated = DateTime.FromBinary((long) objtime).AddTicks(new DateTime(1601, 1, 1).Ticks).ToLocalTime();
            clsevent.EventNewState = eventObject.PropertyValue<UInt32>("EventNewState");
            return clsevent;
        }
    }
}
